function Node(element) {
    this.element = element;
    this.next = null;
    this.previous = null;
    this.currentNode = null;
}

function LList() {
    this.head=  new Node("head");
    this.currentNode = this.head;
}
LList.prototype.find = function(item) {
    var currentNode = this.head;
    while((currentNode.element != item) && (currentNode.next != null)) {
        currentNode = currentNode.next;
    }
    return currentNode;
}
LList.prototype.findPre = function(item) {
    var currentNode = this.head;
    while((currentNode.next != null) && (currentNode.next.element != item)) {
        currentNode = currentNode.next;
    }
    return currentNode;
}
LList.prototype.findAfter = function(item) {
    var currentNode = this.head;
    while((currentNode.element != item) && (currentNode.next != null)) {
        currentNode = currentNode.next;
    }
    return currentNode.next;
}
LList.prototype.findLast = function(item) {
    var currentNode = this.head;
    while(!(currentNode.next == null)) {
        currentNode = currentNode.next;
    }
    return currentNode;
}
LList.prototype.insert = function(newElement, item) {
    var newNode = new Node(newElement);
    var current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}
LList.prototype.remove = function(item) {
    var currentNode = this.find(item);
    if(currentNode.next != null) {
        currentNode.previous.next = currentNode.next;
        currentNode.next.previous = currentNode.previous;
        currentNode.next = null;
        currentNode.previous = null;
    }
}
LList.prototype.display = function() {
    var currentNode = this.head;
    while(!(currentNode.next == null)) {
        console.log(currentNode.next.element);
        currentNode = currentNode.next;
    }
}
LList.prototype.displayReverse = function() {
    var currentNode = this.findLast();
    while(!(currentNode.previous == null)) {
        console.log(currentNode.element);
        currentNode = currentNode.previous;
    }
}

LList.prototype.advance = function(n) {
    for(var i = 0; i < n; i++) {
        if(!(this.currentNode.previous == null)) {
            this.currentNode = this.currentNode.previous;
        }
    }
}
LList.prototype.back = function(n) {
    for(var i = 0; i < n; i++) {
        if(!(this.currentNode.next == null)) {
            this.currentNode = this.currentNode.next;
        }
    }
}
LList.prototype.show = function() {
    console.log('curent node:', this.currentNode.element);
}

var cities = new LList();
cities.insert('Conway', 'head');
cities.insert('Russellville', 'Conway');
cities.insert('Alma', 'Russellville');
cities.insert('Alma1', 'Alma');
cities.insert('Alma2', 'Alma1');
cities.insert('Alma3', 'Alma2');
cities.display();
console.log('back');
cities.back(3);
cities.show();
console.log('advance');
cities.advance(3)
cities.show();

